package cn.itcast.controller;


import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Address;
import cn.itcast.service.BranchSettingSercive;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(path = "/address")
public class BranchSettingController {

    @Reference
    private BranchSettingSercive branchSettingSercive;

    //查询分院信息
    @GetMapping(path = "/findAll")
    public Result findAll() {
        try{
            PageResult pageResult = branchSettingSercive.findAll();
            return new Result(true,MessageConstant.QUERY_ADDRESS_SUCCESS,pageResult.getRows());
        }catch (Exception e){
         return new Result(false,MessageConstant.QUERY_ADDRESS_FAIL);
        }
    }

    //新建分院信息
    @PostMapping (path = "/submit")
    public Result submit(@RequestBody Address address) {
           return branchSettingSercive.submit(address);
    }

    //根据ID查询分院信息
    @GetMapping (path = "/{id}")
    public Result findById(@PathVariable("id") Integer id){
        try{
            Address address = branchSettingSercive.findById(id);
            return new Result(true, MessageConstant.QUERY_ADDRESS_SUCCESS,address);
        }catch (Exception e){
            return new Result(false,MessageConstant.QUERY_ADDRESS_FAIL);
        }
    }

    //删除分院信息
    @DeleteMapping (path = "/{id}")
    public Result deleteById(@PathVariable("id") Integer id){
        try{
            branchSettingSercive.deleteById(id);
            return new Result(true, MessageConstant.DELETE_ADDRESS_SUCCESS);
        }catch (Exception e){
            return new Result(false,MessageConstant.DELETE_ADDRESS_FAIL);
        }
    }

    //更新启用状态
    @GetMapping (path = "updateEnable/{enable}/{id}")
    public Result updateEnable(
            @PathVariable("enable") Integer enable,
            @PathVariable("id") Integer id){
        try{
            branchSettingSercive.updateEnable(id,enable);
            return new Result(true, MessageConstant.EDIT_ADDRESS_SUCCESS);
        }catch (Exception e){
            return new Result(false,MessageConstant.EDIT_ADDRESS_FAIL);
        }
    }
}
